package com.html;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.util.Log;

import com.application.VideosList;
import com.entity.PackNumber;

public class YoukuDetail {
	private Document doc;
	private Context context;
	private VideosList videoList;

	public YoukuDetail(Context context) {
		this.context = context;
		videoList = (VideosList) context.getApplicationContext();
	}

	public String getYoukuDetailInfo(String url) {
		try {

			doc = Jsoup.connect(url).timeout(60000).get();
			List<PackNumber> temp = new ArrayList<PackNumber>();
			Elements content = doc.getElementsByClass("title");
			for (Element element : content) {
				// Log.v("YoukuDetail","title00000000000"+element.text());
				// Log.v("YoukuDetail","-------------------------------------");
				videoList.setYoukuDetailTitle(element.text());
				break;
			}

			Elements content2 = doc.getElementsByClass("pack_number");
			for (Element element : content2) {
				Elements content3 = element.getElementsByTag("li");
				for (Element element2 : content3) {
					// Log.v("YoukuDetail","pack_number00000000000"+element2.text());
					// Log.v("YoukuDetail","pack_number00000000000"+element2.getElementsByTag("a").attr("href"));
					// Log.v("YoukuDetail","-------------------------------------");
					if (!"".equals(element2.getElementsByTag("a").attr("href"))) {
						temp.add(new PackNumber(element2.text(), element2
								.getElementsByTag("a").attr("href")));
					}
				}

			}
			videoList.setPackNumber(temp);
		} catch (Exception e) {
		}
		return "";
	}

	public String getTecentDetailInfo(String url) {
		try {

			doc = Jsoup.connect(url).timeout(60000).get();
			List<PackNumber> temp = new ArrayList<PackNumber>();
			// Elements content =
			// doc.getElementsByAttribute("h1_title");//("mod_path_title");
//			Elements content = doc.getElementsByTag("title");
			// Log.v("QQtitle","%%%%%%%%%%"+content.text().substring(0,
			// content.text().indexOf("-")));
			
//			videoList.setYoukuDetailTitle(content.text().substring(0,
//					content.text().indexOf("-")));
			// Elements content2 =
			// doc.getElementsByTag("ul").addClass("clearfix");
			Elements content2 = doc.getElementsByClass("mod_cont");
			Log.v("QQtitle", "%%%%%%%%%%" + content2.html());
			// for (Element element : content2) {
			Element element = doc.getElementsByClass("mod_cont").get(0);
			Elements content3 = element.getElementsByTag("li");
			for (Element element2 : content3) {
				// Log.v("YoukuDetail","pack_number00000000000"+element2.text());
				// Log.v("YoukuDetail","pack_number00000000000"+element2.getElementsByTag("a").attr("href"));
				// Log.v("YoukuDetail","-------------------------------------");
				try {
					Integer.parseInt(element2.text());
					if (!element2.getElementsByTag("a").attr("href")
							.contains("v.qq")) {
						temp.add(new PackNumber(element2.text(),
								"http://v.qq.com"
										+ element2.getElementsByTag("a").attr(
												"href")));
					} else {
						temp.add(new PackNumber(element2.text(),
								"http://v.qq.com"
										+ element2.getElementsByTag("a").attr(
												"href")));
					}
				} catch (Exception e) {
				}

			}

			// }
			videoList.setPackNumber(temp);
		} catch (Exception e) {
		}
		return "";
	}
}
